/*
 * SPLogout.java
 * 
 * Version information
 * 
 * Apr 10, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.ilink.webapp.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.ilink.webapp.dataObject.LoginData;
import net.amsoft.iservice.ilink.util.ILinkConstants;
import net.amsoft.iservice.isso.client.ISSOClient;
import net.amsoft.iservice.isso.client.SAMLDataObject;
import net.amsoft.iservice.util.ResolverClient;

import org.apache.log4j.Logger;
import org.apache.soap.Envelope;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * The class SPLogout is responsible to handle the Service Provider logout, that
 * means the user logout request will end the current user session for the
 * contact service only but the other active sessions of other services will
 * remain valid. This class does not end a single sign-on session.
 * 
 * @author mohit
 * 
 */

public class SPLogout extends BaseAction {

	private static final Logger oLogger = Logger.getLogger(SPLogout.class);

	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse)
			throws Exception {
		oLogger.info("process(): entry");
		LoginData oLoginData = (LoginData) oRequest.getSession(false)
				.getAttribute(ILinkConstants.SESSION_LOGIN_OBJ);

		ActionMessages oMsgs = new ActionErrors();

		String sSAMLArtifactDoc = oRequest
				.getParameter(ISSOClient.PARAM_SAML_ART);

		String sRelayState = oRequest
				.getParameter(ISSOClient.PARAM_RELAY_STATE);

		SAMLDataObject oReceivedSAMLData = ISSOClient
				.getValuesFromSAMLArtifact(sSAMLArtifactDoc);
		oLogger.debug("process(): SAMLArtifactDoc: " + sSAMLArtifactDoc
				+ ", Relay State: " + sRelayState);

		if (!sRelayState.equals(oLoginData.getRelayState())
				|| !oLoginData.getProviderIDHash().equals(
						oReceivedSAMLData.getProviderIDHash())) {
			oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					ILinkConstants.ERR_UKNOWN_GLOBAL_SESSION));
			saveErrors(oRequest, oMsgs);
			oLogger.warn("process(): invalid RelayState or Artifact");
			return oMapping.findForward(ILinkConstants.FWD_CHKLOGIN);
		}

		String sArtifactConsumerService = ResolverClient.getMetaDataIDP(
				oLoginData.getIDPMetaDataEP())
				.getArtifactResolutionServiceURL();

		String sArtifactResolveXML = ISSOClient
				.createSAMLArtifactResolveRequest(oReceivedSAMLData
						.getArtifact());
		oLogger.debug("process(): ArtifactConsumerService: "
				+ sArtifactConsumerService + ", ArtifactResolveXML: "
				+ sArtifactResolveXML);
		Envelope oEnvelope = ISSOClient.createSOAPEnvelope(sArtifactResolveXML);
		Envelope oReceivedEnvelope = ISSOClient.sendAndReceiveSOAPEnvelope(
				oEnvelope, sArtifactConsumerService);

		oLogger.info("process(): soap envelope received: " + oReceivedEnvelope);
		String sArtifactResponseXML = ISSOClient
				.getArtifactResponseFromSOAPEnvelpe(oReceivedEnvelope);

		String sSAMLStatus = ISSOClient
				.getLogoutSatusFromArtifactResponse(sArtifactResponseXML);

		oLogger.debug("process(): received STATUS:" + sSAMLStatus);

		if (!ISSOClient.SAML_STATUS_SUCCESS.equals(sSAMLStatus)) {
			oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					ILinkConstants.ERR_UKNOWN_GLOBAL_SESSION));
			saveErrors(oRequest.getSession(false), oMsgs);
		} else {
			oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					ILinkConstants.MSG_LOGOUT_SUCCESS));
			saveErrors(oRequest.getSession(false), oMsgs);
		}
		return oMapping.findForward(ILinkConstants.FWD_SUCCESS);
	}
}
